#include "widgetlogin.h"
#include "ui_widgetlogin.h"
#include "MainWidget.h"
#include "qthelper.h"
#include "dialogpwd.h"
#include "qthelper.h"

WidgetLogin::WidgetLogin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetLogin)
{
    ui->setupUi(this);
    this->initForm();
    this->initConfig();
}

WidgetLogin::~WidgetLogin()
{
    delete ui;
}

void WidgetLogin::setDialog(MainWidget *dlg2)
{
    WidgetMain = dlg2;
}


void WidgetLogin::emptyPwd()
{
    ui->lineEditPassword->clear();
    ui->lineEditPassword->setFocus();
}

void WidgetLogin::initForm()
{
    // 获取当前窗口标志
    Qt::WindowFlags winflags = this->windowFlags();
    // 移除最大化按钮标志
    winflags &= ~Qt::WindowMaximizeButtonHint;
    // 应用新的窗口标志
    this->setWindowFlags(winflags);

    this->setWindowTitle("开关机工具");

    //QFont font;
    //font.setPointSize(20); // 设置字体大小为20
    // 将字体应用到QLabel
    //ui->label_title->setFont(font);
    ui->label_title->setStyleSheet("font-size: 20px;");

    ui->lineEditPassword->setEchoMode(QLineEdit::Password);

    QString iniFilePath = qApp->applicationDirPath();
    iniFilePath += "\\"+m_strPathPassword;

    if(m_Settings == NULL)
    {
        m_Settings = new QSettings(iniFilePath, QSettings::IniFormat);
        if (!m_Settings->contains("password")) {
            // 如果不存在，则创建一个默认密码
            QString defaultPassword = "0000";
            m_Settings->setValue("password", defaultPassword);
            m_Settings->sync();
        }
    }
    //    m_Settings
    //    QSettings settings(iniFilePath, QSettings::IniFormat);
    //    // 检查是否存在password键
    //    if (settings.contains("password")) {
    //        // 读取password键的值
    //        QString password = settings.value("password").toString();
    //    } else {
    //        // 如果不存在，则创建一个默认密码
    //        QString defaultPassword = "0000";
    //        settings.setValue("password", defaultPassword);
    //    }

    //    // 修改password键的值
    //    QString newPassword = "new_password";
    //    settings.setValue("password", newPassword);

    //    // 保存更改到INI文件
    //    settings.sync();

}

void WidgetLogin::initConfig()
{
    connect(ui->pushButtonLogin, &QPushButton::clicked, this, &WidgetLogin::onButtonClickedLogin);
    connect(ui->pushButtonPwd, &QPushButton::clicked, this, &WidgetLogin::onButtonChickedPwd);
}

void WidgetLogin::onButtonClickedLogin()
{
    QString strPwd = ui->lineEditPassword->text();
    if (strPwd.isEmpty()) {
        //QMessageBox::warning(this, "提示", "请输入事件名称!");
        QtHelper::showMessageBoxError("请输入登录口令!");
        ui->lineEditPassword->setFocus();
        return;
    }

    QString defaultPassword;
    if (m_Settings!= NULL && m_Settings->contains("password")) {
        defaultPassword = m_Settings->value("password").toString();
    }
    else{
        defaultPassword = "0000";
    }

    if(strPwd != defaultPassword)
    {
        QtHelper::showMessageBoxError("输入口令错误!");
        ui->lineEditPassword->setFocus();
        return;
    }

    if (WidgetMain) {
        WidgetMain->show();
        hide();
    }
}

void WidgetLogin::onButtonChickedPwd()
{
    QString strPassword;
    if(m_Settings != NULL)
    {
        if(m_Settings->contains("password"))
            strPassword = m_Settings->value("password").toString();
        else
            strPassword = "0000";
    }

    DialogPwd addDialog;
    addDialog.setOldPwd(strPassword);

    int result = addDialog.exec();  // 打开对话框
    if (result == QDialog::Accepted) {

        if (m_Settings != NULL){
            m_Settings->setValue("password", addDialog.getPwd());
            m_Settings->sync();
        }

        QtHelper::showMessageBox("口令修改成功!");

        // 处理对话框接受事件
    } else if (result == QDialog::Rejected) {

        //qDebug() << "Rejected:";
        // 处理对话框拒绝事件
    }
}
